﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Browser;
using System.Text;
using System.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCT.UI.ChildWindows;
using Baosight.Platform.Core.ServiceModel;


namespace Baosight.ZNH.ZMCT.UI
{
    public partial class ZMCT10 : EFViewCmes
    {
        public ZMCT10()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
            if (initData.GetBlock("result").GetRowCount() == 0)
            {
                EIInfo eiInfo = new EIInfo();
                EIBlock resultBlock = new EIBlock("result");
                AddColToBlock(resultBlock, "fdKeyId", "主键");
                AddColToBlock(resultBlock, "fdNo", "工号");
                AddColToBlock(resultBlock, "fdName", "姓名");
                AddColToBlock(resultBlock, "fdMobilePhone", "手机号");
                AddColToBlock(resultBlock, "fdDepartmentId", "部门ID");
                eiInfo.AddBlock(resultBlock);
                eiInfo.Set("method", "importPerson");
                eiInfo.Set("service", "ZMCT10");
                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, DoCommitCompleted);
            }
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "人员信息-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "人员信息-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 点击导入人员信息按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public  void OnImportButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            DialogParameters param = this.ModelDialogParameters("Confirm", "导入数据前，将删除原有人员数据!");
            param.Closed = ModifyConfirmClosed;
            EFWindow.Confirm(param);
        }

        /// <summary>
        /// 点击确认按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ModifyConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，“改变状态”按钮生效
                return;
            }
            if (e.DialogResult.Value)
            {
                EIInfo eiInfo = new EIInfo();
                EIBlock block = new EIBlock("result");
                AddColToBlock(block, "fdKeyId", "主键");
                AddColToBlock(block, "fdNo", "工号");
                AddColToBlock(block, "fdName", "姓名");
                AddColToBlock(block, "fdMobilePhone", "手机号");
                AddColToBlock(block, "fdDepartmentId", "部门ID");
                eiInfo.AddBlock(block);
                eiInfo.Set("method", "importPerson");
                eiInfo.Set("service", "ZMCT10");
                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, DoCommitCompleted);
            }
        }

        /// <summary>
        /// 点击"确定"后的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
         private new void  DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.GetStatus() == -1)
            {
                base.OpenEFWindowAlert("导入人员信息失败," + inInfo.GetMsg(), true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                base.OpenEFWindowAlert("导入人员信息成功", true);
            }
            this.TempButtonOpen(true);
            grid.UpdateData(inInfo);
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            PersonAdd systemModule = new PersonAdd(_dialogType);//构造一个新的修改页面
            systemModule.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ct.department"));
            systemModule.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = systemModule;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "人员信息-查看", "detail", null, sender, e);
        }


        /// <summary>
        /// block中插入列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddColToBlock(EIBlock block, string name, string desc)
        {
            EIColumn col = new EIColumn();
            col.SetName(name);
            col.SetDesc(desc);
            block.AddColumn(col);
        }
    }
}
